package com.oa.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Department;
import com.oa.service.DepartmentService;
import com.oa.util.NodeUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")
public class DepartmentAction extends ActionSupport implements
ModelDriven<Department> {
	private static final long serialVersionUID = 1L;

	// 通过注解把 部门服务类 加载进来
	@Resource(name="departmentService")
	private DepartmentService departmentService;

	private Department department = new Department();

	// ================================================================
	
	private String parentId;
	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	// 所有部门
	private List<Department> departmentList;
	public List<Department> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}

	// ===============================以下是业务操作=================

	// 显示 部门
	public String list() {
		
		List<Department> departmentList = null;
		System.out.println(parentId);
		if (parentId == null||"".equals(parentId)) { // 顶级部门列表
			departmentList = departmentService.findTopList();
		} else { // 子部门列表
			departmentList = departmentService.findChildren(parentId);
			Department parent = departmentService.getById(parentId);
			ActionContext.getContext().put("parent", parent);
			System.out.println("parentID:>>>"+parent.getUnique_id());
		}
		ActionContext.getContext().put("departmentList", departmentList);
		return "list";
		
	}

	// 添加 部门
	public String add() {
		
		Department parent = departmentService.getById(parentId);
		System.out.println("parent是：--》"+parent);
		department.setDepartment_parent(parent);
		// 保存
		boolean bl = departmentService.add(department);
		if(bl)
			return "toList";
		else
			return "err";
	}
	
	// 添加 页面
	public String addPage(){
		// 准备数据, departmentList
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = NodeUtil.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);
		
		return "savePage";
	}
	
	// 修改页面
	public String updatePage(){ 
		
		// 准备数据, departmentList
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = NodeUtil.getAllDepartments(topList);
		ActionContext.getContext().put("departmentList", departmentList);

		// 准备回显的数据
		Department department1 = departmentService.getById(department.getUnique_id());
		ActionContext.getContext().put("department1", department1);
		if (department1.getDepartment_parent() != null) {
			parentId = department1.getDepartment_parent() .getUnique_id();
			System.out.println("回显ID=="+parentId);
		}
		
		return "savePage";
	}

	// 删除 部门
	public String delete() {
		System.out.println("部门ID是：》》"+department.getUnique_id());
		boolean bl = departmentService.delete(department);
		if(bl)
			return "toList";
		else
			return "err";
	}

	// 修改 部门
	public String update() {
		
		 // 设置所属的上级部门
		department.setDepartment_parent(departmentService.getById(parentId));
		boolean bl = departmentService.update(department);
		if(bl)
			return "toList";
		else
			return "err";
	}

	// 返回一个模型
	public Department getModel() {
		return department;
	}

}
